/*
 SGTutorialModeViewController.m
 Date Created: March 24, 2010
 
 Author: Karol Krizka <kkrizka@gmail.com>
 
 Copyright (C) 2010 Green-Power
 
 URL: http://code.google.com/p/green-power
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 $Id$
 */

#import "SGTutorialModeViewController.h"

#import "SGBinType.h"

@implementation SGTutorialModeViewController

@synthesize type;

@synthesize titleLabel;
@synthesize descriptionLabel;
@synthesize typeImageView;
@synthesize binImageView;
@synthesize alternateImagesView;
@synthesize tutorialModeSwitch;

- (SGTutorialModeViewController *)initWithType:(SGRecyclableType *)newType {
  self=[super init];
  if(self) {
    type=newType;
    [type retain];
  }
  return self;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  
  titleLabel.text=type.name;
  descriptionLabel.text=type.description;
  
  NSString *imagePath=[NSString stringWithFormat:@"[Original]%@",[type.image objectAtIndex:0]];
  UIImage *image=[UIImage imageNamed:imagePath];
//  typeImageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
  typeImageView.image=image;

  NSArray *bins=[SGBinType listBinTypes];
  for(SGBinType *binType in bins) {
    if([binType.acceptsItems containsObject:type.uniqueName]) {
      image=[UIImage imageNamed:binType.image];
      binImageView.image=image;
    }
  }
  
  NSUInteger MAX_IMAGES_PER_ROW=5;
  CGFloat altImageSize=40; // Square, size = width = height
  CGFloat altImagePadding=(alternateImagesView.frame.size.width-altImageSize*MAX_IMAGES_PER_ROW)/(MAX_IMAGES_PER_ROW-1);
  CGFloat altImageXOffset=altImageSize+altImagePadding; // x = x-offset * col
  CGFloat altImageYOffset=altImageSize+5; // y = y-offset * row
  for(int i=0;i<type.image.count;i++) {
    NSString *altImagePath=[type.image objectAtIndex:i];
    UIImage *altImage=[UIImage imageNamed:altImagePath];
    UIImageView *altImageView=[[UIImageView alloc] initWithImage:altImage];
		altImageView.backgroundColor=[UIColor lightGrayColor];
    altImageView.contentMode=UIViewContentModeScaleAspectFit;
    altImageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
    
    int col=i%MAX_IMAGES_PER_ROW;
    int row=(i-col)/MAX_IMAGES_PER_ROW;
    CGRect altImageFrame=CGRectMake(altImageXOffset*col,
                                    altImageYOffset*row,
                                    altImageSize,
                                    altImageSize);
    altImageView.frame=altImageFrame;
                                    
    
    [alternateImagesView addSubview:altImageView];
    
    [altImageView release];
  }
}

- (IBAction)hide:(id)sender {
  [self dismissModalViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
  [type release];
  [super dealloc];
}


@end
